import { _decorator, Component, Node, Label, Animation, UIOpacity, log } from 'cc';
import { concat, Subscription, Observable } from 'rxjs';
import { ReactiveProperty } from '../../../../scripts/libs/rxjs/cc3/ReactiveProperty';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { nullifySubscription } from '../../../../scripts/libs/rxjs/utils';
import { AnimationPlay } from '../../../../scripts/libs/rxjs/cc3/AnimationPlay';
import { Timer } from '../../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { lm } from '../../LobbyManager';
import { Log, TransparentPass } from '../../../../scripts/libs/rxjs/cc3/CommonObservable';
import { lobby as _uiconfig } from '../../UIConfig';
const { ccclass, property } = _decorator;

@ccclass('FrontTipPanel')
export class FrontTipPanel extends BasePanel {

    message = ReactiveProperty.Create('');

    @property(Node)
    mainNode: Node = null;

    @property(Label)
    messageLabel: Label = null;

    useObserves() {
        return [
            this.message.subscribe(_ => this.messageLabel.string = _),
        ];
    }
    onLoadView() {
        this.reset();
        lm.frontTip = this;
    }

    reset() {
        this.mainNode.getComponent(UIOpacity).opacity = 0;
    }

    _sub: Subscription = null;
    show(message: string, interval = 2) {
        return new Observable(observer => {
            nullifySubscription.call(this, '_sub');
            this.message.value = message;
            this._sub = concat(
                AnimationPlay(this.mainNode.getComponent(Animation), 'fronttip_main_in'),
                Timer(interval, this.node),
                AnimationPlay(this.mainNode.getComponent(Animation), 'fronttip_main_out'),
            ).subscribe({
                complete: () => {
                    observer.next(true);
                    observer.complete();
                }
            });
            return () => {
                this._sub.unsubscribe();
                this.reset();
            }
        });
    }

    public static Show(message: string, interval: number) {
        this.StartProcess(null, _uiconfig.PanelKey.FrontTipPanel as any).subscribe(Log(log));
    }
}

globalThis.FrontTipPanel = FrontTipPanel;

